﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Cashier.usercontrols;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;

namespace Cashier.utils
{
    public class FormUtil
    {
        /// <summary>
        /// 获取窗体中的所有没有验证通过的控件
        /// </summary>
        /// <param name="control"></param>
        /// <param name="list"></param>
        /// <param name="depth"></param>
        public static void GetValidateControlsNotPassed(Control control, ArrayList list, int depth)
        {
            System.Windows.Forms.Control.ControlCollection cls = control.Controls;
            foreach (Control lopControl in cls)
            {
                if (lopControl.HasChildren)
                {
                    GetValidateControlsNotPassed(lopControl, list, depth + 1);
                }
                else
                {
                    if (lopControl is TextboxExt)
                    {
                        TextboxExt textboxExt = lopControl as TextboxExt;
                        if (textboxExt.Isvalidate)
                        {
                            if (!textboxExt.Ispassed)
                            {
                                textboxExt.BackColor = Color.Yellow;
                                list.Add(textboxExt);
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 窗体控件赋值
        /// </summary>
        /// <param name="model"></param>
        /// <param name="control"></param>
        /// <param name="depth"></param>
        public static void InitialFormData(Object model, Control control, int depth)
        {
            System.Windows.Forms.Control.ControlCollection cls = control.Controls;
            foreach (Control lopControl in cls)
            {
                if (lopControl.HasChildren)
                {
                    InitialFormData(model, lopControl, depth + 1);
                }
                else
                {
                    String attrname = lopControl.Name.Substring(lopControl.Name.LastIndexOf("_") + 1).ToLower();
                    Object attrValue = null;
                    PropertyInfo[] properties = model.GetType().GetProperties();
                    foreach (PropertyInfo lopProperty in properties)
                    {
                        if (attrname.Equals(lopProperty.Name.ToLower()))
                        {
                            attrValue = lopProperty.GetValue(model, null);
                            break;
                        }
                    }
                    
                    if (lopControl is TextboxExt)
                    {                       
                        if (attrValue != null)
                        {
                            TextboxExt textboxExt = lopControl as TextboxExt;
                            String passchar = textboxExt.PasswordChar.ToString();
                            if (textboxExt != null && passchar.Equals("\0"))
                                textboxExt.Text = attrValue.ToString();
                        }
                    }
                    else if (lopControl is ComboBox)
                    {
                        ComboBox comboBox = lopControl as ComboBox;
                        if (attrValue != null)
                        {
                            if (comboBox.Tag != null && comboBox.Tag.ToString().Equals("value"))
                                comboBox.SelectedValue = attrValue;
                            else
                                comboBox.Text = attrValue.ToString();
                        }
                    }
                    else if (lopControl is CheckBox)
                    {
                        CheckBox checkbox = lopControl as CheckBox;
                        if (checkbox != null && attrValue.ToString().ToLower().Equals(checkbox.Tag.ToString().ToLower()))
                            checkbox.Checked = true;
                    }
                }
            }
        }

        /// <summary>
        /// 获取控件数据
        /// </summary>
        /// <param name="model"></param>
        /// <param name="control"></param>
        /// <param name="depth"></param>
        public static void GetFormData(Object model, Control control, int depth)
        {
            System.Windows.Forms.Control.ControlCollection cls = control.Controls;
            foreach (Control lopControl in cls)
            {
                if (lopControl.HasChildren)
                {
                    GetFormData(model, lopControl, depth + 1);
                }
                else
                {
                    String attrname = lopControl.Name.Substring(lopControl.Name.LastIndexOf("_") + 1).ToLower();
                    String value = null;
                    if (lopControl is TextboxExt)
                    {
                        TextboxExt textboxExt = lopControl as TextboxExt;
                        value = textboxExt.Text.Trim();
                        if (String.IsNullOrEmpty(value))
                        {
                            if (textboxExt.Datatype == DataType.Double || textboxExt.Datatype == DataType.Integer ||
                                textboxExt.Datatype == DataType.Money || textboxExt.Datatype == DataType.Number)
                            {
                                value = "0";
                            }
                        }
                    }
                    else if (lopControl is ComboBox)
                    {
                        ComboBox comboBox = lopControl as ComboBox;
                        if (comboBox != null)
                        {
                            if (comboBox.Tag != null && comboBox.Tag.ToString().Equals("value") && comboBox.SelectedValue != null)
                                value = comboBox.SelectedValue.ToString();
                            else
                                value = comboBox.Text;
                        }
                    }
                    else if (lopControl is CheckBox)
                    {
                        CheckBox checkbox = lopControl as CheckBox;
                        value = checkbox.Checked.ToString();
                    }

                    if (value != null)
                    {
                        PropertyInfo[] properties = model.GetType().GetProperties();
                        foreach (PropertyInfo lopProperty in properties)
                        {
                            if (attrname.Equals(lopProperty.Name.ToLower()))
                            {
                                String propertyTypeName = lopProperty.PropertyType.Name.ToLower();
                                if (!propertyTypeName.Contains("nullable"))
                                {
                                    if (propertyTypeName.Equals("string"))
                                        lopProperty.SetValue(model, value, null);
                                    else if (propertyTypeName.Equals("int64"))
                                        lopProperty.SetValue(model, Int64.Parse(value), null);
                                    else if (propertyTypeName.Equals("int32"))
                                        lopProperty.SetValue(model, Int32.Parse(value), null);
                                    else if (propertyTypeName.Equals("int16"))
                                        lopProperty.SetValue(model, Int16.Parse(value), null);
                                    else if (propertyTypeName.Equals("decimal"))
                                        lopProperty.SetValue(model, Decimal.Parse(value), null);
                                    else if (propertyTypeName.Equals("boolean"))
                                        lopProperty.SetValue(model, Boolean.Parse(value), null);
                                    else
                                        lopProperty.SetValue(model, value, null);
                                }
                                break;
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 清空表单数据
        /// </summary>
        /// <param name="control"></param>
        /// <param name="depth"></param>
        public static void ClearFormData(Control control, int depth)
        {
            System.Windows.Forms.Control.ControlCollection cls = control.Controls;
            foreach (Control lopControl in cls)
            {
                if (lopControl.HasChildren)
                {
                    ClearFormData(lopControl, depth + 1);
                }
                else
                {
                    if (lopControl is TextboxExt)
                    {
                        TextboxExt textboxExt = lopControl as TextboxExt;
                        if (textboxExt != null)
                        {
                            textboxExt.Text = String.Empty;
                        }
                    }
                    else if (lopControl is ListBox)
                    {
                        ListBox listbox = lopControl as ListBox;
                        if (listbox != null)
                        {
                            listbox.Items.Clear();
                        }
                    }
                    else if (lopControl is CheckBox)
                    {
                        CheckBox checkbox = lopControl as CheckBox;
                        if (checkbox != null)
                        {
                            checkbox.Checked = false;
                        }
                    }
                    else if (lopControl is ComboBox)
                    {
                        ComboBox comboBox = lopControl as ComboBox;
                        if (comboBox != null)
                        {
                            comboBox.SelectedIndex = 0;
                        }
                    }
                }
            }
        }
    }
}
